﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using StopLight.Interfaces;

namespace StopLight.ServiceImplementations {
    public class RealTimeTimer : IStoplightTimer {
        private DispatcherTimer timer;

        public event EventHandler Expired;

        public RealTimeTimer() {
            timer = new DispatcherTimer();
            timer.Tick += OnTick;
            
        }

        public TimeSpan Duration {
            get { return timer.Interval; }
            set { timer.Interval = value; }
        }

        public void Start() {
            timer.Start();
        }

        private void OnTick(object sender, EventArgs e) {
            timer.Stop();
            OnExpired(this);
        }

        protected virtual void OnExpired(object sender) {
            EventHandler handlers = Expired;
            if (handlers != null) {
                handlers(this, EventArgs.Empty);
            }
        }
    }
}
